import { selSmsThemeForMy, selSmsThemeDetails, reSendSmsTheme  } from '@/services/smsTheme';
import { errNotification, successNotification } from '@/utils/notification';
import router from 'umi/router';

export default {
    namespace: 'smsTheme',
  
    state: {
      smsThemeList: [],
      smsThemeListPage: 1,
      smsThemeListTotal: 0,
      
      smsRecordList: [], //全部发送记录
      successList: [],  //成功集合
      errList: [],  //回执错误集合
      noReceipt: [],  //无回执集合
      noReply: [],  //无回复集合
    },
  
    effects: {
      *fetchSelSmsThemeForMy(param, { call, put }) { //查询我的群发短信
        const response = yield call(selSmsThemeForMy, param);
        if(response.success){
            yield put({
                type: 'setSmsThemeList',
                payload: response.data,
            });
        }
      },
      *fetchSelSmsThemeDetails(param, { call, put }) { //查询群发短信详情
        const response = yield call(selSmsThemeDetails, param);
        if(response.success){
            yield put({
                type: 'setSmsThemeDetails',
                payload: response.data,
            });
        }
      },
      *fetchReSendSmsTheme(param, { call, put }) { //查询群发短信详情
        const response = yield call(reSendSmsTheme, param);
        if(response.success){
          successNotification("提交成功","")
          router.push("/sms/smsRecord")
        }else{
          errNotification("提交失败","")
        }
      },
    },
  
    reducers: {
      setSmsThemeList(state, action) {
        if(action.payload.list){
          action.payload.list.map((item,index)=>{
           item.key = item.id
           if(item.children){
            item.children.map((item,index)=>{
              item.key = item.id
            })
           }
          })
        }
        return {
            ...state,
            smsThemeList: action.payload.list,
            smsThemeListPage: action.payload.pageNum,
            smsThemeListTotal: action.payload.total
        };
      },
      setSmsThemeDetails(state, action) {
        let successList = []
        let errList = []
        let noReceipt = []
        let noReply = []
        action.payload.forEach(element => {
          if(element.status === 2){
            successList.push(element)
          }
          if(element.gatewayStatus){
            if(element.gatewayStatus !== "DELIVRD"){
              errList.push(element)
            }
          }else{
            noReceipt.push(element)
          }
          if(!element.replyMsg && element.status === 2){
            noReply.push(element)
          }
        });
        return {
          ...state,
          smsRecordList: action.payload,
          successList: successList,  //成功集合
          errList: errList,  //回执错误集合
          noReceipt: noReceipt,  //无回执集合
          noReply: noReply,  //无回复集合
        };
      },
    },
  };